package com.assignwork.service.impl;

import com.assignwork.dto.WorkArrangeWorkersSubmitDTO;
import com.assignwork.entity.WorkArrangeWorkers;
import com.assignwork.mapper.WorkArrangeWorkersMapper;
import com.assignwork.service.IWorkArrangeWorkersService;
import com.assignwork.vo.WorkArrangeWorkersVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName WorkArrangeWorkersServiceImpl
 * @Description 配工工人接口实现
 * @Author dujianyu
 * @Date 2023/12/13 9:32
 */
@Service
public class WorkArrangeWorkersServiceImpl extends BaseServiceImpl<WorkArrangeWorkersMapper, WorkArrangeWorkers> implements IWorkArrangeWorkersService {

    @Override
    public boolean submitWaWorkers(Long arrangeId, List<WorkArrangeWorkersSubmitDTO> data, String arrangeType) {
        Set<Long> exclude = new HashSet<>();
        if (Func.isNotEmpty(data)) {
            exclude = data.stream().map(WorkArrangeWorkersSubmitDTO::getId).filter(Objects::nonNull).collect(Collectors.toSet());
        }
        boolean removeOldSuccess = removeByArrangeId(arrangeId, exclude);
        if (!Func.isNull(data) && !Func.isEmpty(data) && removeOldSuccess) {
            List<WorkArrangeWorkers> newWorkers = new ArrayList<>();
            for (WorkArrangeWorkersSubmitDTO item : data) {
                WorkArrangeWorkers newWorker = new WorkArrangeWorkers();
                newWorker.setArrangeId(arrangeId);
                newWorker.setWorkers(item.getWorkers());
                newWorker.setTeamId(item.getTeamId());
                newWorker.setArrangeType(arrangeType);
                if (item.getId() != null) {
                    newWorker.setId(item.getId());
                }
                newWorkers.add(newWorker);
            }
            return super.saveOrUpdateBatch(newWorkers);
        } else {
            return removeOldSuccess;
        }
    }

    private boolean removeByArrangeId(Long arrangeId, Set<Long> collect) {
        QueryWrapper<WorkArrangeWorkers> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .select(WorkArrangeWorkers::getId)
                .eq(WorkArrangeWorkers::getArrangeId,arrangeId)
                .notIn(Func.isNotEmpty(collect), WorkArrangeWorkers::getId, collect)
                .eq(WorkArrangeWorkers::getIsDeleted,0);
        List<WorkArrangeWorkers> entities = baseMapper.selectList(queryWrapper);
        if (!Func.isEmpty(entities)) {
            List<Long> ids = entities.stream().map(WorkArrangeWorkers::getId).collect(Collectors.toList());
            return super.deleteLogic(ids);
        }
        return true;
    }

    @Override
    public List<WorkArrangeWorkersVO> infoByArrangeId(Long arrangeId) {
        if (Func.isNull(arrangeId)) {
            throw new RuntimeException("没有配工ID");
        }
        return baseMapper.selectInfoByArrangeId(arrangeId);
    }

    @Override
    public boolean removeByArrangeId(Long arrangeId) {
        QueryWrapper<WorkArrangeWorkers> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .select(WorkArrangeWorkers::getId)
                .eq(WorkArrangeWorkers::getArrangeId,arrangeId)
                .eq(WorkArrangeWorkers::getIsDeleted,0);
        List<WorkArrangeWorkers> entities = baseMapper.selectList(queryWrapper);
        if (!Func.isEmpty(entities)) {
            List<Long> ids = entities.stream().map(WorkArrangeWorkers::getId).collect(Collectors.toList());
            return super.deleteLogic(ids);
        }
        return true;
    }
}
